import UIKit

class StepperView: UIViewController {
    
    // MARK: - 控件属性
    private let stepper = UIStepper()
    private let valueLabel = UILabel()
    private let resetButton = UIButton(type: .system)
    
    // 步进器范围和初始值
    private let minValue: Double = 0
    private let maxValue: Double = 100
    private let initialValue: Double = 50
    private let stepValue: Double = 5
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UIStepper
        stepper.translatesAutoresizingMaskIntoConstraints = false
        stepper.minimumValue = minValue
        stepper.maximumValue = maxValue
        stepper.value = initialValue
        stepper.stepValue = stepValue
        stepper.wraps = false  // 达到最大值时不循环
        stepper.autorepeat = true  // 长按持续改变值
        stepper.tintColor = .systemBlue  // 按钮颜色
        stepper.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged)
        view.addSubview(stepper)
        
        // 创建值标签
        valueLabel.translatesAutoresizingMaskIntoConstraints = false
        valueLabel.font = UIFont.systemFont(ofSize: 24, weight: .semibold)
        valueLabel.textAlignment = .center
        valueLabel.text = "\(Int(initialValue))"
        view.addSubview(valueLabel)
        
        // 创建重置按钮
        resetButton.translatesAutoresizingMaskIntoConstraints = false
        resetButton.setTitle("重置", for: .normal)
        resetButton.backgroundColor = .systemBlue
        resetButton.tintColor = .white
        resetButton.layer.cornerRadius = 8
        resetButton.addTarget(self, action: #selector(resetStepper), for: .touchUpInside)
        view.addSubview(resetButton)
    }
    
    // MARK: - 布局约束
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // Stepper 约束
            stepper.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stepper.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: margin * 3),
            
            // 值标签约束
            valueLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            valueLabel.topAnchor.constraint(equalTo: stepper.bottomAnchor, constant: margin * 2),
            
            // 重置按钮约束
            resetButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            resetButton.topAnchor.constraint(equalTo: valueLabel.bottomAnchor, constant: margin * 3),
            resetButton.widthAnchor.constraint(equalToConstant: 120),
            resetButton.heightAnchor.constraint(equalToConstant: 44)
        ])
    }
    
    // MARK: - 交互逻辑
    @objc private func stepperValueChanged(_ sender: UIStepper) {
        // 获取当前值并更新标签
        let currentValue = Int(sender.value)
        valueLabel.text = "\(currentValue)"
        
        // 可以在这里添加其他响应逻辑，例如改变 UI 元素状态
        updateUIForValue(currentValue)
    }
    
    @objc private func resetStepper() {
        // 重置步进器到初始值
        stepper.value = initialValue
        stepperValueChanged(stepper)  // 触发更新标签
    }
    
    private func updateUIForValue(_ value: Int) {
        // 根据步进器值改变背景色（示例效果）
        let normalizedValue = CGFloat(value) / CGFloat(maxValue)
        let red: CGFloat = 0.8 - (normalizedValue * 0.6)
        let green: CGFloat = 0.4 + (normalizedValue * 0.5)
        view.backgroundColor = UIColor(red: red, green: green, blue: 0.9, alpha: 1.0)
    }
    
}
